package com.cloudptt.api.product.func.sys;

import com.cloudptt.api.product.ISysOperator;
import com.cloudptt.api.product.func.IFunc;
import com.cloudptt.api.product.func.sys.fsm.SysAgent;
import com.cloudptt.api.product.func.sys.listener.ProfileListener;
import com.cloudptt.api.product.func.sys.listener.SysListener;
import com.cloudptt.api.product.func.sys.listener.SysMsgListener;
import com.cloudptt.api.product.log.Log;
import com.cloudptt.api.product.operator.SysOperator;
import com.cloudptt.api.product.timer.RollbackTimer;
import com.mcptt.config.ConfigCenter;
import com.mcptt.config.local.SysConfig;
import com.mcptt.config.model.McIdInfo;
import com.mcptt.config.model.BootStrapSvrInfo;
import com.mcptt.db.DBAdapter;

import android.content.Context;

public class SysFunc  implements IFunc
{
	public final static String Log_Key = "SysFunc";
	
	private Context ctx = null;
	private SysOperator operator = null;
	private SysConfig config = null;
	private SysBroadcast broadcast = null;
	private SysAgent sysAgent = null;
	private DBAdapter dbAdapter = null;
	private RollbackTimer rollbackTimer = null;

	public SysFunc(Context ctx, SysConfig config, SysOperator operator, DBAdapter dbAdapter, IFunc superFunc, ConfigCenter configCenter)
	{
		Log.d(Log_Key, "SysFunc");
		this.ctx = ctx;
		this.config = config;
		this.operator = operator;
		this.dbAdapter = dbAdapter;
		this.sysAgent = new SysAgent(ctx,this.config,this.operator,superFunc,configCenter);
		this.broadcast = new SysBroadcast(ctx,sysAgent,configCenter,config,dbAdapter,operator);
//		sysAgent.doSysAction(SysConst.SysAction_Auth);
	}


	public ISysOperator getSysOperator()
	{
		return sysAgent;
	}
//	public void setLoginListener(LoginListener loginListener) 
//	{
//		this.broadcast.setLoginListener(loginListener);
////		this.operator.setLoginListener(loginListener);
//	}
	
	public void setSysListener(SysListener sysListener) 
	{
		this.sysAgent.setSysListener(sysListener);
	}
	public void setProfileListener(ProfileListener profileListener)
	{
		this.sysAgent.setProfileListener(profileListener);
	}

	public void setSysMsgListener(SysMsgListener sysMsgListener) 
	{
		this.broadcast.setSysMsgListener(sysMsgListener);
	}
	
	
	public SysConfig getConfig() {
		return config;
	}
	public SysOperator getOperator() {
		return operator;
	}
	public SysStateInfo getSysStateInfo()
	{
		Log.debug(this.getClass(), "getSysStateInfo");
		return sysAgent.getSysStateInfo();
	}

	public int getPublishResult() {
		return sysAgent.getPublishResult();
	}
	
	@Override
	public void onLogined(McIdInfo mcIdInfo, BootStrapSvrInfo info, boolean isLoginSuc) {
		// TODO Auto-generated method stub
	}
	@Override
	public void onPublishResult(int publishResult)
	{
		Log.d(SysFunc.Log_Key, "onPublishResult publishResult = " + publishResult);
//		if	(mcType == CallAttr.McType_McData)
//		{
//			Log.d(SysFunc.Log_Key, "onPublishResult send mcType = " + mcType);
////			startCmsGmsTimer();
//		}
	}
	@Override
	public void onSubscribeCmsGmsResult(boolean isSuc,int mcType)
	{}
	@Override
	public void doNetworkChange(boolean isConn)
	{
		Log.d(Log_Key, "doNetworkChange isConn = " + isConn);
		if(!isConn)
		{
//			this.broadcast.clearResourceForLogout();
			this.sysAgent.setNetworkDisconnect(true);
//			this.operator.stopPjsip();
		}
//		else
//		{
//			this.operator.startPjsip();
//		}
	}
	@Override
	public void doForceDetach()
	{
		Log.d(Log_Key, "doForceDetach");
		this.broadcast.clearResourceForLogout();
		this.sysAgent.logout();
		this.operator.logout();
		if(rollbackTimer != null)
		{
			rollbackTimer.cancelTimer();
			rollbackTimer = null;
		}
	}
	@Override
	public void doLogout()
	{
		Log.d(Log_Key, "doLogout");
		this.broadcast.clearResourceForLogout();
		this.sysAgent.logout();
		this.operator.logout();
		this.dbAdapter.resetTables();
		this.config.clearUserInfoAfterLogout();

		if(rollbackTimer != null)
		{
			rollbackTimer.cancelTimer();
			rollbackTimer = null;
		}
	}
	@Override
	public void doExit()
	{
		Log.d(Log_Key, "SysAgent exit");
		this.broadcast.clearResource();
		this.sysAgent.destory();
		this.operator.logout();

		if(rollbackTimer != null)
		{
			rollbackTimer.cancelTimer();
			rollbackTimer = null;
		}

		this.broadcast = null;
		this.sysAgent = null;
		this.operator = null;
		this.config.clearUserInfoAfterExit();
		this.config  = null;
//		this.ctx = null;
	}

	@Override
	public void onLanChange(String lanType) {
		// TODO Auto-generated method stub
		
	}
//	public void setSysAddrListener(SysAddrListener sysAddrListener) {
//		if(sysAgent != null)
//		{
//			sysAgent.setSysAddrListener(sysAddrListener);
//		}
//	}

	@Override
	public void onKeepAlive(boolean isconnected) {
		// TODO Auto-generated method stub
		
	}
}
